﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using omsData;
using myomscl_web.Utils;
namespace myomscl_web.Admin.UserControls
{
    public partial class ucListColumns : System.Web.UI.UserControl
    {

        private Int32? CurrentColumnId
        {
            get
            {
                if (Session["CurrentColumnId"] == null)
                    return null;
                else
                    return Int32.Parse(Session["CurrentColumnId"].ToString());
            }

            set
            {
                Session["CurrentColumnId"] = value;
            }
        }

        #region Public Methods
        public void LoadColumns()
        {
            using (OmsModelContainer context = new OmsModelContainer())
            {
                var columns = context.ListColumns.ToList();

                rptListColumns.DataSource = columns;
                rptListColumns.DataBind();
            }
        }
        #endregion


        #region Private Methods

        private void ShowListDiv()
        {
            DivColumnEdit.Visible = false;
            DivListColumns.Visible = true;
            LoadColumns();
            CurrentColumnId = null;
        }

        private void ShowEditDiv()
        {
            DivColumnEdit.Visible = true;
            DivListColumns.Visible = false;
        }
        #endregion

        #region Event Handlers
        protected void Page_Load(object sender, EventArgs e)
        {            
        }

        protected void btnNewColumn_Click(object sender, EventArgs e)
        {
            txtColumnHeader.Text = string.Empty;
            txtColumnName.Text = string.Empty; 
            CurrentColumnId = null;
            ShowEditDiv();
        }

        protected void rptListColumns_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            using (OmsModelContainer context = new OmsModelContainer())
            {
                CurrentColumnId = Int32.Parse(e.CommandArgument.ToString());
                var col = context.ListColumns.Where(c => c.Id == CurrentColumnId).First();

                if (e.CommandName == "Edit")
                {   
                    
                    txtColumnName.Text = col.Name;
                    txtColumnHeader.Text = col.Header;
                    ShowEditDiv();
                }
                else if (e.CommandName == "Delete")
                {
                    context.ListColumns.DeleteObject(col);
                    context.SaveChanges();
                    ShowListDiv();
                }
            }
        }

        protected void rptListColumns_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ListColumns mgc = (ListColumns)e.Item.DataItem;
                if (mgc != null)
                {
                    var btnEdit = (Button)e.Item.FindControlRecursive("btnEdit");
                    if (btnEdit != null)
                    {
                        btnEdit.CommandArgument = mgc.Id.ToString();
                        btnEdit.Visible = true;
                    }

                    var btnDelete = (Button)e.Item.FindControlRecursive("btnDelete");
                    if (btnDelete != null)
                    {
                        btnDelete.CommandArgument = mgc.Id.ToString();
                        btnDelete.Visible = true;
                    }

                }
            }
        }

        protected void btnSaveNewColumn_Click(object sender, EventArgs e)
        {
            using (OmsModelContainer context = new OmsModelContainer())
            {
                if (CurrentColumnId == null)
                {
                    var newCol = context.ListColumns.CreateObject();
                    newCol.Header = txtColumnHeader.Text;
                    newCol.Name = txtColumnName.Text;
                    context.ListColumns.AddObject(newCol);
                }
                else
                {
                    var col = context.ListColumns.Where(c => c.Id == CurrentColumnId).First();
                    col.Header = txtColumnHeader.Text;
                    col.Name = txtColumnName.Text;
                }

                context.SaveChanges();
                ShowListDiv();
            }

        }

        protected void btnCancelNewColumn_Click(object sender, EventArgs e)
        {
            ShowListDiv();
        }
        #endregion

       
    }
}